package view.Validator;

import javafx.scene.control.Control;
import javafx.scene.control.TextField;

import org.controlsfx.validation.ValidationSupport;
import org.controlsfx.validation.decoration.CompoundValidationDecoration;
import org.controlsfx.validation.decoration.GraphicValidationDecoration;
import org.controlsfx.validation.decoration.StyleClassValidationDecoration;

/**
 * Utility class providing a standard setup for validation of one view.
 * 
 * @version %I%, %G%
 *
 */
public final class MyValidationSupport {

	/**
	 * Mustn't be invoked because it is a utility class.
	 */
	private MyValidationSupport(){
		throw new AssertionError("MyValidationSupport musn't be invoked because it is a utility class.");
	}
	
	/**
	 * @return An instance of <code>ValidationSupport</code> with standard configuration.
	 */
	public static ValidationSupport createDefaultValidationSupport(){
		ValidationSupport valSup = new ValidationSupport();
		StyleClassValidationDecoration cssDecoration = new StyleClassValidationDecoration();
		GraphicValidationDecoration iconDecoration = new GraphicValidationDecoration();
		CompoundValidationDecoration comp = new CompoundValidationDecoration(cssDecoration, iconDecoration);
		valSup.setValidationDecorator(comp);
		
		return valSup;
	}
	
	/**
	 * Checks whether to active the given button or not.
	 * <p>
	 * Checks the given ValidationSupport if all 'required' fields are not empty and if there are no errors.
	 * 
	 * @param btn
	 * @param valSup
	 */
	public static boolean isNodeToDisable(ValidationSupport valSup) {
		boolean isRequiredAndEmpty = false;
		
		for(Control c: valSup.getRegisteredControls()){
			if(ValidationSupport.isRequired(c) && c instanceof TextField){
				isRequiredAndEmpty = isRequiredAndEmpty || ((TextField) c).textProperty().getValue().trim().isEmpty();
			}
		}
		if(valSup.isInvalid() != null){
			return isRequiredAndEmpty || valSup.isInvalid() ;			
		}
		return false;
	}
}
